<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class GoodPrice extends Model
{
    protected $fillable = [
        'id', 'store_id', 'good_id', 'level',
        'price', 'created_at', 'updated_at'
    ];

    public function insertData($data, $storeId, $goodId)
    {
        foreach ($data as $k => $v) {
            $arr['store_id'] = $storeId;
            $arr['good_id'] = $goodId;
            $arr['level'] = $k;
            $arr['price'] = $v * 100;
            $this->create($arr);
        }
    }

    public function updateData($data, $storeId, $goodId)
    {
        foreach ($data as $k => $v) {
            $arr['store_id'] = $storeId;
            $arr['good_id'] = $goodId;
            $arr['level'] = $k;
            $arr['price'] = $v * 100;
            $data = $this->where('store_id', $storeId)->where('good_id', $goodId)->where('level', $k)->first();
            if (empty($data)) {
                $this->create($arr);
            } else {
                $this->where('store_id', $storeId)->where('good_id', $goodId)->where('level', $k)->update($arr);;
            }
        }
    }
}
